package com.topboya.officeuser.controller.parse;

import com.topboya.officeuser.controller.parse.util.OperateExcelUtils;
import com.topboya.officeuser.controller.parse.util.ParseLatAndLngUtil;
import com.topboya.officeuser.utils.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @className PoiHandleController
 * @Author xiaxin
 * @Date 2020/12/31 12:49
 * @Version 1.0
 **/
@RestController
@RequestMapping("handle")
public class PoiHandleController {

    @PostMapping("handleReq")
    public Result handlePoi(@RequestParam("file") MultipartFile multipartFile,@RequestParam("headLine")Integer headLine,@RequestParam("addressPosit")Integer addressPosit,@RequestParam("lngPosit")Integer lngPosit,@RequestParam("latPosit")Integer latPosit,@RequestParam("path")String path) throws Exception {

        OperateExcelUtils operateExcelUtils = new OperateExcelUtils();
        List<List<String>> lists = operateExcelUtils.readList(multipartFile);
        List<List<String>> successList = new ArrayList<>();
        List<List<String>> failList = new ArrayList<>();
        ParseLatAndLngUtil.successAndFailExcelList(headLine, addressPosit, lngPosit, latPosit, lists, successList, failList);

        String name = multipartFile.getOriginalFilename();
        String subName = name.substring(0,name.indexOf("."));
        String successPath = path+subName+"success.xlsx";
        String failPath = path+subName+"fail.xlsx";

        operateExcelUtils.WriteList(successList,successPath);
        operateExcelUtils.WriteList(failList,failPath);

        System.out.println(successList.size());
        System.out.println(failList.size());
        //successList.forEach(System.out::println);
        //failList.forEach(System.out::println);
        //lists.forEach(System.out::println);
        return Result.ok();
    }



}
